package com.ideal.controller;

import com.ideal.service.RefactorMarkdownService;
import com.ideal.request.LocalPathRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 语雀导出格式修复 请求层
 *
 * @author ideal-20
 * @date 2022-10-29 20:43
 **/
@Controller
@RequestMapping("/refactor")
@Slf4j
public class RefactorController {

    @Resource
    private RefactorMarkdownService refactorMarkdownService;

    @PostMapping("/refactorByLocalPath")
    @ResponseBody
    public String useLocalFilePath(LocalPathRequest localPath) {
        log.info("useLocalPath request localPath:{}", localPath);
        boolean checkFlag = checkLocalPathParam(localPath);
        if (!checkFlag) {
            return "param error";
        }
        boolean ret = refactorMarkdownService.doRefactorLocal(localPath);
        return ret ? "OK" : "Error";
    }

    private boolean checkLocalPathParam(LocalPathRequest req) {
        if (StringUtils.isBlank(req.getLocalPath()) || req.getPathType() == null) {
            return false;
        }
        return true;
    }

}